/*
 * Created on Jan 16, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.uploadapi.flickrrest.ui;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.MenuItem;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.JUploadr;
import org.scohen.juploadr.app.PhotoSet;
import org.scohen.juploadr.app.UploadImage;
import org.scohen.juploadr.ui.PopupData;
import org.scohen.juploadr.ui.PopupHandler;
import org.scohen.juploadr.uploadapi.flickrrest.Messages;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class UploadThumbnailPopupHandler extends PopupHandler {
    private static final String ADD_TO_SET = org.scohen.juploadr.uploadapi.flickrrest.Messages
            .getString("juploadr.flickrrest.ui.popup.add.to"); //$NON-NLS-1$
    private static final String A_NEW_SET = org.scohen.juploadr.uploadapi.flickrrest.Messages
            .getString("juploadr.flickrrest.ui.popup.add.to.new.set"); //$NON-NLS-1$
    private static String REMOVE_PHOTO = Messages
            .getString("juploadr.ui.popup.remove.photo"); //$NON-NLS-1$
    private static String EDIT_PHOTO = Messages
            .getString("juploadr.ui.popup.edit.photo"); //$NON-NLS-1$
    private static String SELECT_PHOTO = Messages
            .getString("juploadr.ui.popup.select.photo"); //$NON-NLS-1$
    private static String DESELECT_PHOTO = Messages
            .getString("juploadr.ui.popup.deselect.photo"); //$NON-NLS-1$
    private static String RE_UPLOAD_PHOTO = Messages
            .getString("juploadr.ui.popup.reupload.photo"); //$NON-NLS-1$
    private ImageAttributes atts;
    private List photoSets;

    public UploadThumbnailPopupHandler(Decorations parent, JUploadr uploadr,
            ImageAttributes clicked, List photoSets) {
        super(parent);
        this.atts = clicked;
        this.photoSets = photoSets;
        show();
    }

    protected void widgetSelected(SelectionEvent event) {

        MenuItem src = (MenuItem) event.widget;
        String action = src.getText();
        if (REMOVE_PHOTO.equals(action)) {
            atts.removeFromWindow();
        } else if (EDIT_PHOTO.equals(action)) {
            atts.openEditUI();
        } else if (SELECT_PHOTO.equals(action)) {
            atts.setSelected(true);
            atts.getButton().redraw();
        } else if (DESELECT_PHOTO.equals(action)) {
            atts.setSelected(false);
            atts.getButton().redraw();
        } else if (RE_UPLOAD_PHOTO.equals(action)) {
            atts.setState(UploadImage.STATE_DATA_ENTERED);
            atts.getButton().redraw();
        } else if (A_NEW_SET.equals(action)) {
            NewPhotoSetDialog dialog = new NewPhotoSetDialog(JUploadr.window,
                    atts);
            dialog.open();
        } else {
            // they have selected add to photoset
            MenuItem item = (MenuItem) event.widget;
            PhotoSet set = (PhotoSet) item.getData();
            List alreadyAdded = atts.getPhotoSets();
            if (alreadyAdded.contains(set)) {
                // remove the set if it's already there
                set.removePhoto(atts);
            } else {
                set.addPhoto(atts);
            }
        }

    }

    protected void regenerateOptions() {
        options = new LinkedList<PopupData>();
        options.add(new PopupData(EDIT_PHOTO, true));
        if (atts.isSelected()) {
            options.add(new PopupData(DESELECT_PHOTO, true));
        } else {
            options.add(new PopupData(SELECT_PHOTO, true));
        }
        if (atts.getState() == UploadImage.STATE_UPLOADED) {
            options.add(new PopupData(RE_UPLOAD_PHOTO, true));
        } else {
            options.add(new PopupData(RE_UPLOAD_PHOTO, false));
        }
        options.add(PopupData.SEPARATOR);
        options.add(new PopupData(REMOVE_PHOTO, true));
        List alreadyAdded = atts.getPhotoSets();

        options.add(PopupData.SEPARATOR);
        if (photoSets != null && photoSets.size() > 0) {
            PopupData addToSet = new PopupData(ADD_TO_SET, true);
            options.add(addToSet);
            for (int i = 0; i < photoSets.size(); i++) {
                PhotoSet photoSet = (PhotoSet) photoSets.get(i);
                PopupData data = new PopupData(photoSet.getTitle(), photoSet,
                        true);
                data.setType(SWT.CHECK);
                data.setChecked(alreadyAdded.contains(photoSet));
                addToSet.addChild(data);
                if (i > 0 && i % MAX_SUBMENU_SIZE == 0) {
                    PopupData next = new PopupData(
                            org.scohen.juploadr.uploadapi.flickrrest.Messages
                                    .getString("juploadr.flickrrest.ui.popup.more"), true); //$NON-NLS-1$
                    addToSet.getChildren().add(0, next);
                    addToSet.getChildren().add(1, PopupData.SEPARATOR);
                    addToSet = next;

                }
            }
        }
        options.add(new PopupData(A_NEW_SET, true));
    }
}